const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const passport = require("passport");

// 引入users.js
const users = require("./routers/Users");
// 引入census.js
const census = require("./routers/Census");
// 引入blog.js
const blog = require("./routers/Blog");
// 引入UpdateImg.js
const updateimg = require("./routers/UpdateImg");

const app = express();

// 设置静态资源目录
const path = require("path");
app.use("/images", express.static(path.join(__dirname, "images")));

// 使用body-parser中间件
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

// 使用routes
// 注册users中间件
app.use("/api/users", users);
// 注册census中间件
app.use("/api/census", census);
// 注册blog中间件
app.use("/api/blog", blog);
// 注册updateimg中间件
app.use("/api/img", updateimg);

// 使用passport
// passport初始化
app.use(passport.initialize());
// 配置passport
require("./config/passport")(passport);

// 引入mongodburl
const db = require("./config/mongodb").mongoURL;
// 连接数据库
mongoose
	.connect(db)
	.then(() => {
		console.log("mongodb 连接成功");
	})
	.catch((err) => console.log(err));

const port = 5001;
app.listen(port, () => {
	console.log(`server running on port ${port}`);
});
